package com.alison.importselectorcondition;

import com.alison.domain.BeanA;
import com.alison.domain.BeanB;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

/**
 * @Author alison
 * @Date 2024/3/27 13:24
 * @Version 1.0
 * @Description
 */
public class ImportSelectorConditionImpl implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
//        return new String[]{"com.alison.domain.BeanA"};
        String[] strArr = new String[0];
        String val = null;
        Set<String> annotationTypes = importingClassMetadata.getAnnotationTypes();
        Iterator<String> classIter = annotationTypes.iterator();
        while (classIter.hasNext()) {
            String type = classIter.next();
            if (type.equals(EnableImportSelectorCondition.class.getName())) {
                Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableImportSelectorCondition.class.getName());
                val = (String) annotationAttributes.get("value");
                break;
            }
        }
        if (BeanA.class.getSimpleName().equalsIgnoreCase(val)) {
            strArr = new String[]{BeanA.class.getName()};
        } else if (BeanB.class.getSimpleName().equalsIgnoreCase(val)) {
            strArr = new String[]{BeanB.class.getName()};
        }
        return strArr;
    }

    @Override
    public Predicate<String> getExclusionFilter() {
        return ImportSelector.super.getExclusionFilter();
    }
}
